package lol.clann.minecraft.springboot.api.utils.asm;

import org.springframework.asm.ClassReader;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.FieldVisitor;
import org.springframework.asm.SpringAsmInfo;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 直接读取类的字段或者方法,可能遇到类不存在的异常
 *
 * 这里提供ASM的方式读取
 *
 * @author pyz
 * @date 2019/5/12 11:15 PM
 */
public class ASMUtils {

    private static Map<String, List<String>> fieldNameMap = new ConcurrentHashMap<>();
    private static Map<String, List<Field>> fieldMap = new ConcurrentHashMap<>();

    public static List<String> getDeclaredFieldNames(Class clazz) throws IOException {
        List<String> infos = fieldNameMap.get(clazz.getName());
        if (infos != null) {
            return infos;
        }
        synchronized (fieldNameMap) {
            infos = fieldNameMap.get(clazz.getName());
            if (infos == null) {
                infos = new ArrayList<>();
                try {
                    for (Field field : clazz.getDeclaredFields()) {
                        infos.add(field.getName());
                    }
                } catch (Throwable e) {
                    infos = getDeclaredFieldNames(clazz.getName());
                }
                fieldNameMap.put(clazz.getName(), infos);
            }
        }
        return infos;
    }

    /**
     * 使用ASM读取类中字段信息
     *
     * @param className
     * @return
     * @throws IOException
     */
    public static List<String> getDeclaredFieldNames(String className) throws IOException {
        List<String> infos = fieldNameMap.get(className);
        if (infos != null) {
            return infos;
        }
        synchronized (fieldNameMap) {
            infos = fieldNameMap.get(className);
            if (infos == null) {
                infos = new ArrayList<>();
                ClassReader classReader = new ClassReader(ASMUtils.class.getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class"));
                List<String> infos0 = infos;
                ClassVisitor visitor = new ClassVisitor(SpringAsmInfo.ASM_VERSION) {
                    @Override
                    public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
                        infos0.add(name);
                        return super.visitField(access, name, descriptor, signature, value);
                    }
                };
                classReader.accept(visitor, ClassReader.SKIP_DEBUG);
                fieldNameMap.put(className, infos);
            }
        }
        return infos;
    }

    /**
     * 使用ASM读取类中字段信息
     *
     * @param clazz
     * @return
     * @throws IOException
     */
    public static List<Field> getDeclaredFields(Class clazz) throws IOException, NoSuchFieldException {
        List<Field> fields = fieldMap.get(clazz.getName());
        if (fields != null) {
            return fields;
        }
        synchronized (fieldMap) {
            fields = fieldMap.get(clazz.getName());
            if (fields == null) {
                fields = new ArrayList<>();
                List<String> names = getDeclaredFieldNames(clazz);
                for (String name : names) {
                    try {
                        fields.add(clazz.getDeclaredField(name));
                    } catch (Throwable e) {
                        continue;
                    }

                }
                fieldMap.put(clazz.getName(), fields);
            }
        }
        return fields;
    }

}
